import { getTagList ,getTagDetail} from '@/services';
import { ITagListType,ITagDetailList} from '@/types';
import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';

export interface TagModelState {
    tagList: ITagListType [],
    tagDetailList:ITagDetailList[]
}

export interface TagModelType {
  namespace: 'tagList';
  state: TagModelState;
  effects: {
    getTagList: Effect;
    getTagDetail:Effect;
  };
  reducers: {
    save: Reducer<TagModelState>;
  };
}

const TagModel: TagModelType = {
  // 数据模型，用来隔离数据防止相互影响
  namespace: 'tagList',

  // 数据模型状态，相当于vuex的state
  state: {
    tagList: [],
    tagDetailList:[]
  },

  // 异步操作，相当于vuex的action
  effects: {
    // 先简单记忆，*相当于async，yield相当于await
    *getTagDetail({payload}, {call, put}){
        let result = yield getTagDetail(payload);
        if (result.data){
          yield put({
            type: 'save',
            payload: {
            tagDetailList: result.data,
            }
          })
        }
      },
    *getTagList({payload}, {call, put}){
      let result = yield getTagList();
      if (result.data){
        yield put({
          type: 'save',
          payload: {
            tagList: result.data,
          }
        })
      }
    }
  },

  // 同步操作，相当于vuex的mutation
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },

  }
};

export default TagModel;